<?php
namespace backend\controllers;

use backend\models\AuthGroupAccess;
use common\components\BaseController;
use Yii;
use yii\db\Query;
use backend\models\User;
use backend\models\Admin;
use backend\models\AuthGroup;
use \common\helps\tools;
use \common\helps\code;
use \common\models\UploadForm;
use yii\web\UploadedFile;


/**
 * Admin controller
 */
class AdminController extends BaseController
{

    public $modelClass = 'backend\models\Admin';
    public function init()
    {
//        $this->enableAuth=false;
        parent::init();
    }

    /**
     * 获取后台用户列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetUserList(){
        $request =$this->request;
        $model = new Admin();
        $params = $request->queryParams;
        $total_data = $model->getUserList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
        $tableHeader = ['id','用户名','角色','姓名','手机号'];
        tools::ToPHPEXECL($tableHeader,$total_data,'后台用户列表');
        }


        return code::success($data);

    }

    /**
     * 后台用户编辑信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionUserInfoEdit(){
        $request =$this->request;
        $model = new Admin();
        if($request->isPost){
            $params = $request->bodyParams;
            if(empty($params['id']))   return code::error('','参数错误');
            $model->scenario = 'user_info';
            if($model->load($params,'')){
                if($model->validate()){
                    $model::updateUserInfo($this->request->bodyParams,$params['id']);
                    if(empty($model->getErrors())){
                        return code::success('','编辑成功!');
                    }else{
                        return code::$error;
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }


        }
    }

    /**
     * 获取后台用户的信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionGetUserInfo(){
        $model = new Admin();
        $user_info = $model->getUserInfo($this->user_id);
        unset($user_info['password']);
        unset($user_info['salt']);
        unset($user_info['token']);
        $user_info['userAuthMenu'] = $model->getAuthRuleArray($this->user_id);
        return code::success($user_info);
    }



    /**
     * 后台用户修改密码
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionModifyPassword(){
        $model = new Admin();
        $model->scenario = 'modify';
        if($model->load($this->request->bodyParams,'')){
            if($model->validate()){
                $params=$this->request->bodyParams;
                 $model::modifyPassword($params,$params['id']);
                if(empty($model->getErrors())){
                    return code::success('','修改密码成功!');
                }else{
                    return code::$error;
                }
            }else{
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
        }


    }




    /**
     * 新增后台登录用户
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionUserAdd(){
        $request =$this->request;
        $model = new Admin();
        if($request->isPost){
            $params = $request->bodyParams;
            if($model->load($params,'')){
                if($model->validate()){
                    $params['salt'] = tools::generateRandomString(4);
                    $params['password'] = sha1(md5($params['password'].$params['salt']));
                    $res = $model->saveUserInfo('create',$params);
                    if(empty($res->getErrors())){
                        return code::success('','新增用户成功!');
                    }else{
                        return code::$error;
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }

    }

    /**
     * 获取用户权限角色
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetAuthRole(){
        $role_model = new AuthGroup();
        $param=$this->request->bodyParams;
        if(empty($param['type'])) return code::error('','参数错误');
        $data_list = $role_model->getAuthList($param);
        return code::success($data_list);
    }

    /**
     * 用户上传头像
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionAvatarUpload()
    {
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $files = $_FILES['file'];
            if(empty($files)){
                return code::error('','未获取到文件对象');
            }
            $model->file =$files;
            $dir=UPLOADS.'user/';
            $res = $model->upload($dir,$files);
            if($res){
                    return code::success(array('data'=>array('file_name'=>$res)),'上传成功');
            }else{
                return code::error('','上传失败');
            }
        }

        return code::error('','操作错误');
    }


    /**
     * 授权用户角色权限接口
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionAddUserAccess()
    {
        if($this->request->isPost) {
            $model = new AuthGroupAccess();
            $params = $this->request->bodyParams;
            $model->scenario = 'auth';
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $model->addAccess($params,intval($params['uid']));
                    if (empty($model->getErrors())) {
                        return code::success('', '新增用户成功!');
                    } else {
                        return code::$error;
                    }
                } else {
                    $error = array_values($model->getErrors());
                    return code::error('', $error[0][0]);
                }
            }
        }

    }

    /**
     * 删除用户
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDeleteUser(){
        if($this->request->isPost) {
            $admin_model = new Admin();
            $params = $this->request->bodyParams;
            if(empty($params['user_id'])){
                return code::error('','参数错误');
            }
            $user_id= intval($params['user_id']);
            $res = $admin_model->deleteUser($user_id);
            if($res){
                return code::success();
            }else{
                return code::error('','操作失败');
            }

        }

    }

    /**
     * 获取指定用户的信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionGetTargetUserInfo(){
        $model = new Admin();
        $request =$this->request;
        $params = $request->queryParams;
        if(empty($params['id']))   return code::error('','参数错误');
        $user_info = $model->getUserInfo(intval($params['id']));
        unset($user_info['password']);
        unset($user_info['salt']);
        unset($user_info['token']);
        return code::success($user_info);
    }



    /**
     * 后台登录用户编辑信息(不添加权限)
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionUserSelfInfoEdit(){
        $request =$this->request;
        $model = new Admin();
        if($request->isPost){
            $params = $request->bodyParams;
            $model->scenario = 'user_info';
            if($model->load($params,'')){
                if($model->validate()){
                    $model::updateUserInfo($this->request->bodyParams,$this->user_id);
                    if(empty($model->getErrors())){
                        return code::success('','编辑成功!');
                    }else{
                        return code::$error;
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }


        }
    }

    /**
     * 后台登录用户修改密码(不添加权限)
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionModifySelfPassword(){
        $model = new Admin();
        $model->scenario = 'modify';
        $params=$this->request->bodyParams;
        $params['id'] = intval($this->user_id);
        if($model->load($params,'')){
            if($model->validate()){
                $model::modifyPassword($params,$this->user_id);
                if(empty($model->getErrors())){
                    return code::success('','修改密码成功!');
                }else{
                    return code::$error;
                }
            }else{
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
        }


    }







}
